/**
 * 我是校长-课程列表 JS 交互
 */
var IsAuthorize = typeof(IsAuthorize) == "undefined" ? false : IsAuthorize;
var authorize_str = IsAuthorize ? "&authorize=1&sid="+SID : "";
$(function() {
	$("#search_box").inputTips();
	
	//课程目录
	$("a.course_content").click(function(){
		var courseid = $(this).attr('courseid');
		var courseContent = $.ckAjaxBoxy({
			url : '/?mod=seller&act=course&do=content&courseid='+courseid+authorize_str,
			title : "查看课程目录",
			width : 768,
			height : 592
		});
		return false;
	});
	
	//编辑课程标题
	$("a.editTitle").click(function(){
		var obj = $(this),
			courseid = obj.attr('courseid'),
			title = obj.attr('cname'),
			boxy = $.ckBoxy({
				content	: '<div class="d_d_bjkc"><input type="text" class="form_text" style="width:290px;margin-top:13px;"><span style="display:none" class=" form_error"></span></div><div class="tc pt10"><a href="javascript:;" class="button n_btn38g mr20">确定</button><a href="javascript:;" class="cancel n_btn38gr_b"><span>取消</span></a></div>',
				title	: '修改标题',
				width	: 354,
				height	: 234,
				callback : function(){
					box = $("#fe_dialogBox");
					box.find("input[type=text]").val(title);
					box.find("a.cancel").click(function(){
						boxy.cancel();
					});
					box.find("a.button").click(function(){
						var button = $(this),
							name = box.find("input[type=text]").val();
						//box.find("span.form_error").text('').show();
						$.ajax({
							url : KK._kkurl+"/?mod=seller&act=course&do=editTitle"+authorize_str,
							type : "post",
							data : "courseid="+courseid+"&name="+encodeURIComponent(name),
							dataType : "json",
							async : false,
							beforeSend : function(){
								button.attr('disabled', 'disabled');
							},
							success : function(rs){
								button.removeAttr('disabled');
								if(rs.code > 0){
									window.location.reload();
									boxy.cancel();
								}else{
									box.find("span.form_error").text(rs.data).show();
									box.find("textarea").focus();
								}
							}
						});
						return false;
					});
				}
			});
		return false;
	});
	
	//停售课程
	$("a.offsell").click(function(){
		var courseid = $(this).attr('courseid');
		var loadingBox = '';
		$.ajax({
			url : KK._kkurl+"/?mod=seller&act=course&do=offSale&courseid="+courseid+"&random="+Math.random()+authorize_str,
			type : 'get',
			dataType : 'json',
			async : false,
			beforeSend : function(){
				loadingBox = $.ckTipsBoxy({type:"loading", mask:true});
			},
			success : function(rs){
				if(rs.code > 0){
					$.ckTipsBoxy({type:"success",message:"操作成功",callback:function(){
						document.location.reload();
					}});
				}else{
					loadingBox.cancel();
					$.ckAlert({message:rs.data});
				}
			}
		});
		return false;
	});
	
	//发布课程
	$("a.onsell").click(function(){
		var courseid = $(this).attr('courseid');
		var loadingBox = '';
		$.ajax({
			url : KK._kkurl+"/?mod=seller&act=course&do=onSale&courseid="+courseid+"&random="+Math.random()+authorize_str,
			type : 'get',
			dataType : 'json',
			async : false,
			beforeSend : function(){
				loadingBox = $.ckTipsBoxy({type:"loading", mask:true});
			},
			success : function(rs){
				if(rs.code > 0){
					$.ckTipsBoxy({type:"success",message:"发布成功",callback:function(){
						document.location.reload();
					}});
				}else{
					loadingBox.cancel();
					$.ckAlert({message:rs.data});
				}
			}
		});
		return false;
	});
	
	//删除课程
	$("a.recycle").click(function(){
		var obj = $(this);
		var courseid = obj.attr('courseid');
		$.ckConfirm({
			message : "删除课程将清除可能含有的未结束直播课安排，且不可恢复，是否继续？",
			ok : function(){
				$.ajax({
					url : KK._kkurl+"/?mod=seller&act=course&do=recycle&confirm=1&courseid="+courseid+"&rand="+Math.random()+authorize_str,
					async : false,
					dataType : "json",
					type : "get",
					success : function(rs){
						if(rs.code > 0){
							obj.parents("tr:eq(0)").fadeOut('normal', function(){
								$(this).remove();
							})
						}else{
							$.ckAlert({message : rs.data});
						}
					}
				});
			} 
		});
		return false;
	});
	
	//恢复课程
	$("a.recover").click(function(){
		var obj = $(this);
		var courseid = obj.attr('courseid');
		$.ckConfirm({
			message : "你确定要恢复该课程吗？",
			ok : function(){
				$.ajax({
					url : KK._kkurl+"/?mod=seller&act=course&do=recover&courseid="+courseid+"&rand="+Math.random()+authorize_str,
					async : false,
					dataType : "json",
					type : "get",
					success : function(rs){
						if(rs.code > 0){
							obj.parents("tr:eq(0)").fadeOut('normal', function(){
								$(this).remove();
							})
						}else{
							$.ckAlert({message : rs.data});
						}
					}
				});
			} 
		});
		return false;
	});
	
	//彻底删除课程
	/*$("a.delete").click(function(){
		var obj = $(this);
		var courseid = obj.attr('courseid');
		$.ckConfirm({
			message : "删除后将不可恢复，你确定删除此课程？",
			ok : function(){
				$.ajax({
					url : KK._kkurl+"/?mod=seller&act=course&do=delete&courseid="+courseid+"&rand="+Math.random()+authorize_str,
					async : false,
					dataType : "json",
					type : "get",
					success : function(rs){
						if(rs.code > 0){
							obj.parents("tr:eq(0)").fadeOut('normal', function(){
								$(this).remove();
							})
						}else{
							$.ckAlert({message : rs.data});
						}
					}
				});
			} 
		});
		return false;
	});*/
});